// @ 516
// _(:з」∠)_
// 模拟 标签
// elements

const Elmt = {};

// 模拟 select
// simulation select
Elmt['select'] = function( obj ) {
    if ( obj && obj.elmt && obj.data ) {
        let selectItem = ``,
            selectValue = obj.elmt.querySelector("h"),
            selectContent = obj.elmt.querySelector("ul");

        let data = objToHeavy( obj.data, "name" );

        for ( let x = 0; x < data.length; x++ ) {
            selectItem += `<li value="${ data[x].value }">${ data[x].name }</li>`;
        }
        
        selectValue.textContent = data[0].name;
        selectValue.setAttribute("value", data[0].value);

        selectContent.textContent = "";
        selectContent.insertAdjacentHTML("beforeEnd", selectItem);
        selectItem = null;
    }

    if ( !obj.type ) selectComOpt( obj.elmt );
}

Elmt["observer"] = function( element ) {
    let elmtObs = new MutationObserver( function( records ) {
        records.map( function( record ) {
            if ( record.target.getAttribute("value") != -1 ) {
                
           } else {
                
           }
        });
    });
    elmtObs.observe( element, { 'childList': true, 'arrtibutes': true } );
}

// 通用事件
function selectComOpt( elmt ) {
    // 基础选择隐藏
    elmt.addEventListener( 'click', function(e) {
        let selectEvent = elmt.querySelector("h"),
            ulList = selectEvent.nextElementSibling;

        if ( e.target.nodeName == "H" ) {
            if ( ulList.hasClass('select') ) {
                ulList.removeClass('select');
            } else {
                ulList.addClass('select');
            }
        } else if ( e.target.nodeName == "LI" ) {
            ulList.removeClass('select');
            selectEvent.textContent = e.target.textContent;
            selectEvent.setAttribute( "value", e.target.getAttribute("value") );
        }
    });

    // 点击其他隐藏
    document.body.addEventListener( 'click', function(e) {
        let ulList = elmt.querySelector("ul");
        if ( ulList.hasClass("select") && !e.target.isChildren( elmt ) ) {
            ulList.removeClass("select");
        }
    }, false);
}